<?php

!defined('IN_HC') && exit('Misquoted');

/**
* redis 类
*/
class mredis
{
	private	$rc = 0;   
	private	$redis;
	private	$redisIp;
	private	$redisPort;
	private	$redisSelect;
    private $redisIsConnect = FALSE;

	function __construct($redisIp, $redisPort, $redisSelect = 1)
	{
		$this->redisIp = $redisIp;
		$this->redisPort = $redisPort;
		$this->redisSelect = $redisSelect;

		$this->inRedis();
	}

    final function inRedis()
    {
        if(empty($this->redis) || !is_object($this->redis))
        {
            $this->redis = new Redis();
            $this->redisIsConnect = FALSE;            
        }
    }

	final function isRedis()
	{
        $this->inRedis();

		if(!$this->redisIsConnect)
		{
			$connect = (!$this->rc ? 'p' : '') . 'connect';
            
            $this->redis->$connect($this->redisIp, $this->redisPort);
            $this->redis->select($this->redisSelect);
            
            $this->rc++;
            $this->redisIsConnect = TRUE;
		}

		return $this->redis;
	}

	// 处理操作不存在
    PUBLIC function & __call($fn, $arguments)
    {
    	$result = NULL;

        $this->isRedis();

        if(method_exists($this->redis, $fn))
        {
        	$result = call_user_func_array(array($this->redis, $fn), $arguments);
        }
        
        return $result;
    }
	
}



